---全局单例时间管理（延时调用函数，可调用次数，是否一直循环调用）前需要把此脚本的Update函数放入Unity生命周期FixedUpdate中

TimeManager = BaseClass("TimeManager", Singleton);
TimeManager.callback = {}
---添加延时调用函数
---action事件,timeNumber延时事件(s为单位),isLoop是否循环,isInfiniteLoop是否无限循环,loopNumber循环调用的次数
function TimeManager:AddDelayAction(action, timeNumber, isLoop, isInfiniteLoop, loopNumber, ...)
    if not action then
        print("事件为空")
        return
    end
    local parameters = ...
    ---如果不是循环则直接设置一次延时
    if not isLoop then
        local idelayAction = setTimeout(function()
            action(parameters)
            clearTimeout(idelayAction)
        end, timeNumber * 1000)
        return
    end
    self.callback[action] = {}
    self.callback[action].action = action
    self.callback[action].isInfiniteLoop = isInfiniteLoop
    self.callback[action].timeNumber = timeNumber
    self.callback[action].loopNumber = loopNumber
    self.callback[action].parameters = ...
    self.callback[action].currentTimeNumber = 0
    self.callback[action].currentLoopNumber = 0
end

---移除延时调用函数,只能用在循环延时调用的事件(isImmediate是否立刻停止延时调用，如果非立刻停止,将在下一次循环调用完成后停止)
function TimeManager:RemoveDelayAction(action, isImmediate)
    if isImmediate then
        self.callback[action] = nil
    else
        self.callback[action].isImmediate = isImmediate
    end
end

function TimeManager:Update()
    for key, value in pairs(self.callback) do
        value.currentTimeNumber = value.currentTimeNumber + UnityEngine.Time.deltaTime
        if not value.isInfiniteLoop then
            if value.currentLoopNumber == value.loopNumber then
                self.callback[key]=nil
            end
        end
        if value then
            if value.currentTimeNumber >= value.timeNumber then
                value.action(value.parameters)
                value.currentLoopNumber = value.currentLoopNumber + 1
                value.currentTimeNumber = 0
                if  value.isImmediate==false then
                    self.callback[key]=nil
                end
            end
        end
    end
end

local function __init(self)
end

local function __delete(self)
end
TimeManager.__init = __init
TimeManager.__delete = __delete
